package com.haiertvbic.tvnow.pop;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.haiertvbic.tvnow.OTTInfoActivity;
import com.haiertvbic.tvnow.R;
import com.haiertvbic.tvnow.adapter.ResProgramAdapter;
import com.haiertvbic.tvnow.model.OTTInfo;
import com.haiertvbic.tvnow.model.ThridProgram;
import com.haiertvbic.tvnow.network.EPGWeb;
import com.haiertvbic.tvnow.testblock.Function;
import com.haiertvbic.tvnow.testblock.TestData;
import com.haiertvbic.tvnow.util.Util;

public class ProgramJSPop extends PopWindow {

	private static final String TAG = "ProgramJSPop";
	private Activity context;
	private OTTInfo resource;
	private View childView;
	private PopupWindow pop;

	public ProgramJSPop(Activity context, View childView, OTTInfo resource) {
		this.context = context;
		this.childView = childView;
		this.resource = resource;
	}

	@Override
	public void showPop() {
		// TODO Auto-generated method stub
		new JSAsyncTask().execute(resource);
	}

	@Override
	public void dismissPop() {
		if (pop != null && pop.isShowing()) {
			pop.dismiss();
			pop = null;
		}
	}

	public class JSAsyncTask extends AsyncTask<Object, Void, Void> {
		private OTTInfo resource;
		private ArrayList<ThridProgram> thridPrograms;
		ViewHodler hodler = new ViewHodler();

		class ViewHodler {
			ProgressBar pb;
			GridView JsGV;
			ListView JsLv;
			TextView toMore;
		}

		@Override
		protected void onPreExecute() {
			View connvert = LayoutInflater.from(context).inflate(
					R.layout.pop_ott_js_show, null);
			int[] arrayOfInt = new int[2];
			if (childView != null) {
				childView.getLocationInWindow(arrayOfInt);
				float pictureHeight = context.getResources().getDimension(
						R.dimen.picture_height);
				float pictureWidth = context.getResources().getDimension(
						R.dimen.picture_width);
				float screen = context.getWindowManager().getDefaultDisplay()
						.getWidth();

				// 当条目被选中弹出popwindow
				pop = new PopupWindow(connvert, (int) pictureWidth * 2,
						(int) (pictureHeight + 0.5f));
				pop.setBackgroundDrawable(new BitmapDrawable());
				pop.setFocusable(true);
				pop.showAtLocation(childView, Gravity.LEFT | Gravity.TOP,
						arrayOfInt[0], arrayOfInt[1]);
				hodler.JsGV = (GridView) connvert
						.findViewById(R.id.programJsGv);
				hodler.JsLv = (ListView) connvert
						.findViewById(R.id.program_js_lv);
				hodler.pb = (ProgressBar) connvert
						.findViewById(R.id.progressBar_js);
				hodler.toMore = (TextView) connvert.findViewById(R.id.to_more);
				ScaleAnimation sa = new ScaleAnimation(0.4f, 1.0f, 0.4f, 1.0f,
						screen, 0f);
				sa.setDuration(800);
				connvert.startAnimation(sa);
				hodler.pb.setVisibility(View.VISIBLE);
				hodler.JsGV.setVisibility(View.GONE);
				hodler.JsLv.setVisibility(View.GONE);
			}
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Object... params) {
			resource = (OTTInfo) params[0];
			String moreUrl = resource.getMore();
			thridPrograms = EPGWeb.paserThridProgram(moreUrl);
			if (Function.testData.get("OttInforJs")) {
				thridPrograms = TestData.setTestJsData(thridPrograms);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			if (hodler != null) {
				hodler.pb.setVisibility(View.GONE);
				hodler.toMore.setVisibility(View.VISIBLE);
				if (thridPrograms != null && thridPrograms.size() > 0) {
					ResProgramAdapter pJsAdapter = new ResProgramAdapter(
							context, thridPrograms);
					// 判断是电视剧还是不是,根据集数判断
					if (Util.isJs(thridPrograms)) {
						hodler.JsGV.setVisibility(View.VISIBLE);
						hodler.JsLv.setVisibility(View.GONE);
						hodler.JsGV.setAdapter(pJsAdapter);
						hodler.JsGV.setFocusable(true);
						hodler.JsGV.requestFocus();
						hodler.JsGV
								.setOnItemClickListener(new ProgramSetOnItemClickListener(
										resource, thridPrograms));
						hodler.JsGV.setOnKeyListener(new PopOnKeyListener());
					} else {
						hodler.JsGV.setVisibility(View.GONE);
						hodler.JsLv.setVisibility(View.VISIBLE);
						hodler.JsLv.setAdapter(pJsAdapter);
						hodler.JsLv.setFocusable(true);
						hodler.JsLv.requestFocus();
						hodler.JsLv
								.setOnItemClickListener(new ProgramSetOnItemClickListener(
										resource, thridPrograms));
						hodler.JsLv.setOnKeyListener(new PopOnKeyListener());
					}
				} else {
					dismissPop();
					Toast.makeText(context,
							context.getString(R.string.sorry_no_data), 1)
							.show();
				}
			}

			super.onPostExecute(result);
		}
	}

	public class ProgramSetOnItemClickListener implements OnItemClickListener {
		private OTTInfo resource;
		private ArrayList<ThridProgram> thridPrograms;

		public ProgramSetOnItemClickListener(OTTInfo resource,
				ArrayList<ThridProgram> thridPrograms) {
			this.resource = resource;
			this.thridPrograms = thridPrograms;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			ThridProgram thridProgram = thridPrograms.get(arg2);
			new OTTInfoActivity().callApk(thridProgram, resource, context);

		}

	}

	public class PopOnKeyListener implements OnKeyListener {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_BACK:
				pop.setFocusable(false);
				dismissPop();
				break;

			default:
				break;
			}
			return false;
		}

	}

	@Override
	public boolean getPopState() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setPopState(boolean b) {
		// TODO Auto-generated method stub

	}

}
